# Contributor: Marian <marian.buschsieweke@posteo.net>
# Maintainer: Marian <marian.buschsieweke@posteo.net>
pkgname=micropython
pkgver=1.24.0
_mpy_lib_ver=1.24.0
pkgrel=0
pkgdesc="lean and efficient Python implementation for MCUs and constrained systems"
url="https://www.micropython.org/"

# ppc64le: mpy-cross fails to compile with "unsupported relocation type 4 & 5"
arch="all !ppc64le"

license="MIT"
makedepends="libffi-dev python3"
subpackages="$pkgname-cross $pkgname-doc"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/micropython/micropython/archive/refs/tags/v$pkgver.tar.gz
	micropython-lib-$_mpy_lib_ver.tar.gz::https://github.com/micropython/micropython-lib/archive/refs/tags/v$_mpy_lib_ver.tar.gz
	micropython-prevent-stripping.patch
	no_ssl.patch
	no_legacy_berkeley_db.patch
	no-werror.patch
	"

prepare() {
	default_prepare
	rmdir lib/micropython-lib
	ln -s ../../micropython-lib-$_mpy_lib_ver lib/micropython-lib
}

build() {
	make -C mpy-cross PYTHON=python3
	make -C ports/unix PYTHON=python3
}

check() {
	# Likely caused by float rounding not being super precise. For aarch64
	# this was tracked and closed under:
	# https://github.com/micropython/micropython/issues/4176
	case "$CARCH" in
		aarch64|riscv64|loongarch64)
			rm tests/float/float_parse.py tests/float/float_parse_doubleprec.py
			;;
		s390x)
			rm tests/float/float_parse.py tests/float/float_parse_doubleprec.py \
			tests/extmod/asyncio_fair.py tests/cmdline/repl_*
			# repl_basic.py seems to consistently fail, others occasionally
			;;
	esac

	# Needed for tests/extmod/select_poll_fd.py to
	# pass (see comment in that file for more info)
	ulimit -n 1024

	make -C ports/unix test PYTHON=python3
}

package() {
	install -Dm0755 -t "$pkgdir"/usr/bin \
		ports/unix/build-standard/micropython \
		mpy-cross/build/mpy-cross

	install -Dm0644 -t "$pkgdir"/usr/share/doc/"$pkgname"/ \
		LICENSE docs/unix/quickref.rst
	install -Dm0644 -t "$pkgdir"/usr/share/doc/"$pkgname"/reference/ \
		docs/reference/*
}

cross() {
	pkgdesc="Compile micropython code to bytecode, which can be transfered to MCUs"

	amove usr/bin/mpy-cross
}

sha512sums="
a94db76d02fe0886d53105e69b6866d6873dcb7de48628408c92378e7039b08daa254ec0bf54802418e4af7469b45d9a68bb97dcb61ec58bf163225751de3d30  micropython-1.24.0.tar.gz
4443f9e0bf3519bff066bed355a3987510ccaf432897e7878d8984a1cffcf7ec0aa5fb7577b23cbee79b92fafa573e918c9b601693c111bfd7b29ac387daa31c  micropython-lib-1.24.0.tar.gz
cf5454c2d28b44b029cc80b5a2a84f91e2405fd4fe79e20265b31b36b57598f25cc2e8cdbd0f4db75d8d7e7734271d245bd8346c85eddf673691934b9c7d8d7a  micropython-prevent-stripping.patch
6e13449a4d273538d9d752de88a292ff15ef035ca55abc8c5ee4b396d3088c292f059e59a9b17594d02393a0deb28f309d6fde057ba9c2bd91e58b99f55d7c97  no_ssl.patch
e86ad02ef746605f2f685ae776a6ad6ea39ae6e57fa0869a0c03895fd581647de20fa7e6aaeebac45e7302a63451d625208b1356ee2f35a66df70ac2755a25ff  no_legacy_berkeley_db.patch
99a021dc8fe8f7cd26455e9e7610e36bfb3c01182b42855a6cc3453dea29ea25fe622c3d722a71f973a1382446f6434260cc2a986d92895e187e784c57069b31  no-werror.patch
"
